package com.captjack.micro.dao.database.base;

import java.lang.reflect.Field;
import java.util.ArrayList;
import java.util.List;

/**
 * @author Administrator
 */
public class GenerateSql {

    public static void main(String[] args) {
    }

    /**
     * 将驼峰式命名转换为下划线式命名
     *
     * @param str
     * @return
     */
    public static String change(String str) {
        List<Integer> list = new ArrayList<Integer>();
        List<String> list1 = new ArrayList<String>();
        int count = 0;
        for (int i = 0; i < str.length() + count; i++) {
            if ((int) str.charAt(i) <= 90) {
                list.add(i);
            }
        }
        if (list.size() > 0) {
            for (int i = 0; i <= list.size(); i++) {
                if (i == 0) {
                    list1.add(str.substring(0, list.get(0)) + "_");
                } else if (i == list.size()) {
                    list1.add(str.substring(list.get(list.size() - 1)));
                } else {
                    list1.add(str.substring(list.get(i - 1), list.get(i)) + "_");
                }
            }
            StringBuilder stringBuilder = new StringBuilder();
            for (String string : list1) {
                stringBuilder.append(string);
            }
            return stringBuilder.toString().toUpperCase();
        } else {
            return str.toUpperCase();
        }
    }

    /**
     * 由entity生成sql
     *
     * @param obj     需要生成的类
     * @param preWord 前缀，表的别名
     * @return
     */
    public static String returnSql(Object obj, String preWord) {
        StringBuffer sql = new StringBuffer("SELECT ");
        StringBuffer sql1 = new StringBuffer(" FROM " + obj.getClass().getName() + " " + preWord);
        // 这里的fields包含versionId，所以entity的所有字段应该是（fields.length-1）个
        Field[] fields = obj.getClass().getDeclaredFields();
        for (int i = 1; i < fields.length; i++) {
            if (i != fields.length - 1) {
                sql.append(preWord).append(".").append(change(fields[i].getName())).append(", ");
            } else {
                sql.append(preWord).append(".").append(change(fields[i].getName()));
            }
        }
        return sql.append(sql1).toString();
    }

    /**
     * 生成RowMapper
     *
     * @param className
     * @param obj
     * @return
     */
    public static String returnMapRow(String className, Object obj) {
        StringBuffer sql = new StringBuffer("");
        // 得到所有的字段
        Field[] fields = obj.getClass().getDeclaredFields();
        // 拼接字符串
        for (int i = 0; i < fields.length; i++) {
            if ("class java.lang.String".equals(fields[i].getType().toString())) {
                sql.append("else if ( \"" + change(fields[i].getName()).toUpperCase() + "\".equals(colName) ) { "
                        + className + ".set" + String.valueOf(fields[i].getName().charAt(0)).toUpperCase()
                        + fields[i].getName().substring(1) + "(rs.getString(\""
                        + change(fields[i].getName()).toUpperCase() + "\" ));}");
            } else if ("class java.lang.Integer".equals(fields[i].getType().toString())
                    || "int".equals(fields[i].getType().toString())) {
                sql.append("else if ( \"" + change(fields[i].getName()).toUpperCase() + "\".equals(colName) ) { "
                        + className + ".set" + String.valueOf(fields[i].getName().charAt(0)).toUpperCase()
                        + fields[i].getName().substring(1) + "(rs.getInt(\"" + change(fields[i].getName()).toUpperCase()
                        + "\" ));}");
            } else if ("class java.lang.Long".equals(fields[i].getType().toString())
                    || "long".equals(fields[i].getType().toString())) {
                sql.append("else if ( \"" + change(fields[i].getName()).toUpperCase() + "\".equals(colName) ) { "
                        + className + ".set" + String.valueOf(fields[i].getName().charAt(0)).toUpperCase()
                        + fields[i].getName().substring(1) + "(rs.getLong(\""
                        + change(fields[i].getName()).toUpperCase() + "\" ));}");
            } else if ("class java.lang.Boolean".equals(fields[i].getType().toString())
                    || "boolean".equals(fields[i].getType().toString())) {
                sql.append("else if ( \"" + change(fields[i].getName()).toUpperCase() + "\".equals(colName) ) { "
                        + className + ".set" + String.valueOf(fields[i].getName().charAt(0)).toUpperCase()
                        + fields[i].getName().substring(1) + "(rs.getBoolean(\""
                        + change(fields[i].getName()).toUpperCase() + "\" ));}");
            } else {
                sql.append("else if ( \"" + change(fields[i].getName()).toUpperCase() + "\".equals(colName) ) { "
                        + className + ".set" + String.valueOf(fields[i].getName().charAt(0)).toUpperCase()
                        + fields[i].getName().substring(1) + "(rs.getObject(\""
                        + change(fields[i].getName()).toUpperCase() + "\" ));}");
            }
        }
        return sql.toString().substring(5);
    }

}
